﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HTComics.Web.Common
{
    public class Pricing
    {
        public static double CalculatePrice(double nmmPrice, int conditionId)
        {
            double sellPrice = 0;
            double forexRate = 1;
            PriceBandCollection pbColl = (PriceBandCollection)HttpContext.Current.Application["PriceBands"];
            PriceGuideCollection pgColl = (PriceGuideCollection)HttpContext.Current.Application["PriceGuides"];
            PriceGuide guide = pgColl[pbColl[nmmPrice].BandId, conditionId];

            if (conditionId == 3) //If NM-, return same price back as all prices entered are in NM-
                sellPrice = nmmPrice;
            //
            else if (conditionId == 1 || conditionId == 2) //NM+ Or NM, return the product of Factor in DB to NM-
                sellPrice = nmmPrice * guide.Factor.Value;
            else
            {
                if (guide.ConditionId_2 == null) // If VF, F, VG or G
                {
                    sellPrice = (guide.VarA.Value + (guide.VarB.Value / nmmPrice)) * nmmPrice;
                }
                else
                {
                    //VF+, VF-, F+, F-, VG+, VG-, G+, G-, FR, P
                    PriceGuide guide2 = pgColl[pbColl[nmmPrice].BandId, guide.ConditionId_2.Value];

                    sellPrice = (guide2.VarA.Value + (guide2.VarB.Value / nmmPrice)) * nmmPrice;
                    sellPrice *= guide.Factor.Value;
                }
            }

            forexRate = Convert.ToDouble(HttpContext.Current.Application["ForexRate"]);
            return sellPrice * forexRate;
        }
    }
}